package com.shenhaofeng.app.http.request;

import com.shenhaofeng.app.http.Headers;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * Created by Administrator on 2016/10/13.
 */
public class MultipartBody extends RequestBody {

    private ArrayList<Part> entities;

    private MediaType mediaType;

    private String boundary;


    public MultipartBody(List<Part> entities, MediaType mediaType, String boundary) {
        this.entities = (entities == null ? new ArrayList<Part>() : new ArrayList<>(entities));
        this.mediaType = mediaType;
        this.boundary = boundary;
    }


    public ArrayList<Part> entities() {
        return (ArrayList<Part>) entities.clone();
    }


    public String boundary() {
        return boundary;
    }


    @Override
    public MediaType contentType() {
        return mediaType;
    }


    public static class Part {


        private Headers headers;

        private String name;

        private String filename;

        private RequestBody requestBody;


        private Part(Headers headers, String name, String filename, RequestBody requestBody) {
            this.headers = headers;
            this.name = name;
            this.filename = filename;
            this.requestBody = requestBody;
        }


        public String name() {
            return name;
        }


        public String filename() {
            return filename;
        }


        public Headers headers() {
            return headers;
        }


        public RequestBody requestBody() {
            return requestBody;
        }


        public static Part create(String name, String value) {
            return new Part(null, name, null, new ValueBody.Builder().value(value).build());
        }


        public static Part create(String name, String filename, FileBody fileBody) {
            return new Part(null, name, filename, fileBody);
        }


        public static Part create(Headers headers, RequestBody fileBody) {
            return new Part(headers, null, null, fileBody);
        }


        public static Part create(RequestBody fileBody) {
            return new Part(null, null, null, fileBody);
        }
    }



    public static class Builder {

        private List<Part> entities = new ArrayList<>();

        private MediaType mediaType = MediaType.create("multipart/form-data");

        private String boundary = UUID.randomUUID().toString();


        public Builder boundary(String boundary) {
            this.boundary = boundary;
            return this;
        }


        public Builder addEntity(Headers headers, RequestBody body) {
            entities.add(Part.create(headers, body));
            return this;
        }


        public Builder addEntity(RequestBody body) {
            return addEntity(null, body);
        }


        public Builder addEntity(String name, String filename, FileBody fileBody) {
            entities.add(Part.create(name, filename, fileBody));
            return this;
        }


        public Builder addEntity(String name, String value) {
            entities.add(Part.create(name, value));
            return this;
        }


        public Builder mediaType(MediaType mediaType) {
            this.mediaType = mediaType;
            return this;
        }


        public MultipartBody build() {
            return new MultipartBody(entities, mediaType, boundary);
        }
    }


}
